package com.ti.shangou.controller.pages.back.user;

import com.ti.shangou.controller.BaseController;
import com.ti.shangou.dao.UserDao;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Role;
import com.ti.shangou.pojo.entity.User;
import com.ti.shangou.pojo.query.UserQuery;
import com.ti.shangou.pojo.vo.RoleVO;
import com.ti.shangou.pojo.vo.UserVO;
import com.ti.shangou.service.ImgCacheService;
import com.ti.shangou.service.UserService;
import com.ti.shangou.service.impl.ImgCacheServiceImpl;
import com.ti.shangou.service.impl.RoleServiceImpl;
import com.ti.shangou.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/pages/back/user")
public class UserController extends BaseController {
    @Resource
    RoleServiceImpl roleService;
    @Resource
    UserServiceImpl userService;
    @Resource
    UserDao userDao;
    @Resource
    ImgCacheServiceImpl imgCacheService;

    @RequestMapping("list")
    String list(Model model) {
        List<RoleVO> systemRoles = roleService.getSystemRoles();
        model.addAttribute("systemRoles",systemRoles);
        return "pages/back/user/user-list";
    }

    @ResponseBody
    @RequestMapping("ajaxList")
    Object userList(UserQuery userQuery){
        PageDTO pageDTO = userService.ajaxList(userQuery);
        return pageDTO;
    }

    @ResponseBody
    @RequestMapping("getUserRoles/{phone}")
    public Object getUserRoles(@PathVariable String phone){
        List<RoleVO> roleVOS = userService.selectHisRolesByPhone(phone);
        return roleVOS;
    }

    @ResponseBody
    @RequestMapping("addUser")
    public Object addUser(User user){
        user.setUserId(getUserId());
        if (userDao.insertSelective(user)==1){
            imgCacheService.deleteImgCache(user);
            return ResponseDTO.ok("添加成功");
        }
      return ResponseDTO.fail("添加失败");
    }

    /**
     * 权限分配接口
     */
    @ResponseBody
    @RequestMapping("dispatchUserPermission/{userId}")              // 使用role接受，没有permissionVo集合
    public ResponseDTO dispatchUserPermission(@PathVariable Long userId, @RequestBody List<Role> roleList){
        return userService.dispatchUserPermission(userId, roleList);
    }

    @ResponseBody
    @RequestMapping("deleteUsers")
    public ResponseDTO deleteUsers(@RequestBody List<UserVO> userVOS){

        return ResponseDTO.get(userService.deleteUsers(userVOS)==userVOS.size());
    }




}
